package io.renren.modules.oa.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import io.renren.common.validator.ValidatorUtils;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.oa.entity.OaPlanEmployeeEntity;
import io.renren.modules.oa.redis.DingtalkRedis;
import io.renren.modules.oa.service.OaPlanEmployeeService;
import io.renren.modules.sys.shiro.ShiroUtils;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.utils.dingtalk.DingtalkUtils;



/**
 * 值班员工
 *
 * @author ziye
 * @email 524748907@qq.com
 * @date 2018-10-25 11:56:08
 */
@RestController
@RequestMapping("oa/oaplanemployee")
public class OaPlanEmployeeController {
    @Autowired
    private OaPlanEmployeeService oaPlanEmployeeService;
    
    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("oa:oaplanemployee:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = oaPlanEmployeeService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("oa:oaplanemployee:info")
    public R info(@PathVariable("id") Long id){
        OaPlanEmployeeEntity oaPlanEmployee = oaPlanEmployeeService.selectById(id);

        return R.ok().put("oaPlanEmployee", oaPlanEmployee);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("oa:oaplanemployee:save")
    public R save(@RequestBody OaPlanEmployeeEntity oaPlanEmployee){
        oaPlanEmployeeService.insert(oaPlanEmployee);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("oa:oaplanemployee:update")
    public R update(@RequestBody OaPlanEmployeeEntity oaPlanEmployee){
        ValidatorUtils.validateEntity(oaPlanEmployee);
        oaPlanEmployeeService.updateAllColumnById(oaPlanEmployee);//全部更新
        
        return R.ok();
    }
    
    @RequestMapping("/updateSort")
    public R updateSort(@RequestParam Long id,@RequestParam Integer sort){
    	OaPlanEmployeeEntity oaPlanEmployee = oaPlanEmployeeService.selectById(id);
        oaPlanEmployee.setSort(sort);
        oaPlanEmployeeService.updateAllColumnById(oaPlanEmployee);//全部更新
        Long merchantId = ShiroUtils.getUserId();	//商户编号
        DingtalkRedis.delEmployeeList(merchantId);
    	DingtalkRedis.delActiveList(merchantId);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("oa:oaplanemployee:delete")
    public R delete(@RequestBody Long[] ids){
        oaPlanEmployeeService.deleteBatchIds(Arrays.asList(ids));
        Long merchantId = ShiroUtils.getUserId();	//商户编号
    	DingtalkRedis.delEmployeeList(merchantId);
    	DingtalkRedis.delActiveList(merchantId);
        return R.ok();
    }
    
    /**
     * 设置值班状态
     * @param ids
     * @param active
     * @return
     */
    @RequestMapping("/setActive")
    @RequiresPermissions("oa:oaplanemployee:update")
    public R setActive(@RequestBody Long[] ids){
    	List<OaPlanEmployeeEntity> entityList = oaPlanEmployeeService.selectBatchIds(Arrays.asList(ids));
    	for (OaPlanEmployeeEntity oaPlanEmployeeEntity : entityList) {
    		oaPlanEmployeeEntity.setActive(1);
		}
    	oaPlanEmployeeService.updateBatchById(entityList);
    	Long merchantId = ShiroUtils.getUserId();	//商户编号
    	DingtalkRedis.delEmployeeList(merchantId);
        return R.ok();
    }
    
    /**
     * 取消值班
     * @param ids
     * @param active
     * @return
     */
    @RequestMapping("/cancelActive")
    @RequiresPermissions("oa:oaplanemployee:update")
    public R cancelActive(@RequestBody Long[] ids){
    	List<OaPlanEmployeeEntity> entityList = oaPlanEmployeeService.selectBatchIds(Arrays.asList(ids));
    	for (OaPlanEmployeeEntity oaPlanEmployeeEntity : entityList) {
    		oaPlanEmployeeEntity.setActive(0);
		}
    	oaPlanEmployeeService.updateBatchById(entityList);
    	Long merchantId = ShiroUtils.getUserId();	//商户编号
    	DingtalkRedis.delEmployeeList(merchantId);
        return R.ok();
    }
    
  
    
    @RequestMapping("/updateEmployeeList")
    @RequiresPermissions("oa:oaplanemployee:employee")
    public R updateEmployeeList(){
    	Long merchantId = ShiroUtils.getUserId();	//商户编号
    	String accessToken = DingtalkRedis.getAccessToken(merchantId);
    	if(StringUtils.isNotEmpty(accessToken)){
    		JSONArray userIds = DingtalkRedis.getDeptMember(accessToken, "1");  
    		for (int i = 0; i < userIds.length(); i++) {
    			String userid = userIds.getString(i);
    			JSONObject member = DingtalkRedis.getMember(accessToken, userid);
    			if(member != null){
    				String name = member.getString("name");
    				String avatar = "";
    				if(member.has("avatar")){
    					avatar = member.getString("avatar");
    				}
    				String mobile = "";
    				if(member.has("mobile")){
    					mobile = member.getString("mobile");
    				}
    				OaPlanEmployeeEntity employeeEntity = oaPlanEmployeeService.selectByUserid(merchantId, userid);
    				if(employeeEntity == null){
    					employeeEntity = new OaPlanEmployeeEntity();
    					employeeEntity.setCreateTime(new Date());
    					employeeEntity.setActive(0);	//未激活
    					employeeEntity.setMerchantId(merchantId);
        				employeeEntity.setUserid(userid);
    				}
    				employeeEntity.setName(name);
    				employeeEntity.setAvatar(avatar);
    				employeeEntity.setMobile(mobile);
    				employeeEntity.setUpdateTime(new Date());
    				if(!(employeeEntity.getSort() != null && employeeEntity.getSort() > 0)){
    					employeeEntity.setSort(0);
    				}
    				oaPlanEmployeeService.insertOrUpdate(employeeEntity);
    			}
			}
    		DingtalkRedis.delEmployeeList(merchantId);
    		return R.ok();
    	}else{
    		return R.error("全局配置异常， 请查看");
    	}
    	//DingtalkUtils.getAccessToken(appKey, appSecret);
        //oaPlanEmployeeService.updateEmployeeList();

        
    }
    
    @RequestMapping("/queryEmployeeList")
    @RequiresPermissions("oa:oaplanemployee:list")
    public R queryEmployeeList(@RequestParam Map<String, Object> params){
    	Long merchantId = ShiroUtils.getUserId();	//商户编号
        List<OaPlanEmployeeEntity> list = DingtalkRedis.queryActiveList(merchantId);
        return R.ok().put("employeeList", list);
    }

}
